package com.ai.service.impl;

import com.ai.dao.mapper.BfmUserMapper;
import com.ai.service.OperationLogService;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class OperationLogServiceImpl implements OperationLogService {

    @Autowired
    private BfmUserMapper bfmUserMapper;

    public List<JSONObject> getOperationLogs(SimpleDateFormat simpleDateFormat, String now) {
        List<Map<String, Object>> operationLogs = bfmUserMapper.getOperationLogs();
        List<JSONObject> jsonObjects = new ArrayList<>();
        for (Map<String, Object> operationLogMap : operationLogs) {
            JSONObject jsonObject = new JSONObject();   // 操作日志信息
            JSONObject jsonObject2 = new JSONObject();  // 日志通用字段
            JSONObject jsonObject3 = new JSONObject();  // 汇总JSON
            String id = MapUtils.getString(operationLogMap, "id");
            jsonObject.put("logId", id);
            jsonObject.put("clientIp", MapUtils.getString(operationLogMap, "res_container_host"));
            jsonObject.put("clientMac", "");
            jsonObject.put("clientType", "WEB");
            jsonObject.put("clientDeviceId", "");
            jsonObject.put("account", MapUtils.getString(operationLogMap, "user_code"));
            jsonObject.put("phone", MapUtils.getString(operationLogMap, "phone"));
            jsonObject.put("unit", "【天翼云】大数据和AI中心");
            jsonObject.put("unitId", "8500066");
            jsonObject.put("department", "");
            jsonObject.put("position", "");
            jsonObject.put("businessIp", "127.0.0.1");
            jsonObject.put("businessSystemName", "【天翼云】中国电信重庆公司信息服务业务系统AI通用平台");
            jsonObject.put("businessSystemId", "10069");
            String create_time = simpleDateFormat.format(operationLogMap.get("create_time"));
            String update_time = simpleDateFormat.format(operationLogMap.get("update_time"));
            String instance_status = MapUtils.getString(operationLogMap, "instance_status");
            String name = MapUtils.getString(operationLogMap, "name");
            if (create_time.equals(update_time)) {
                jsonObject.put("operate_type", "1");
                jsonObject.put("operate_desc", "【天翼云】新增notebook实例" + name + "成功");
                jsonObject.put("operate_command", "INSERT INTO `ai_factory_center_new`.`vm_instance` (`id`, `name`, `instance_status`, `create_time`, `update_time`)  VALUES (" + id + ",'" + name + "','" + "'0'" + "','" + create_time + "','" + update_time + ");");
            } else if ("1".equals(instance_status)) {
                jsonObject.put("operate_type", "2");
                jsonObject.put("operate_desc", "【天翼云】删除notebook实例" + name + "成功");
                jsonObject.put("operate_command", "delete from ai_factory_center_new.vm_instance where id=" + id + ";");
            } else {
                jsonObject.put("operate_type", "3");
                jsonObject.put("operate_desc", "【天翼云】修改notebook实例" + name + "成功");
                jsonObject.put("operate_command", "update ai_factory_center_new.vm_instance set 'name'=" + name + ",update_time=" + update_time + " where id=" + id);
            }
            jsonObject.put("operate_time", create_time);
            jsonObject.put("result", "1");
            jsonObject.put("operate_param", "");
            jsonObject.put("operate_business", "notebook实例");
            jsonObject.put("operate_level", "1");
            jsonObject.put("operate_sensitive", "0");
            jsonObject.put("operate_reason", "0");
            jsonObject.put("operate_reason_desc", "");
            jsonObject.put("operate_vn", "");
            jsonObject.put("SensitiveLevel", "D6-1");
            jsonObject.put("ExportStaff", "");
            jsonObject.put("ExportTime", "");
            jsonObject.put("ExportFileName", "");

            jsonObject2.put("log_province", "85");
            jsonObject2.put("logTime", create_time);
            jsonObject2.put("logID", MapUtils.getString(operationLogMap, "id"));
            jsonObject2.put("Log_type", "026");
            jsonObject2.put("equ_manuf", "");
            jsonObject2.put("dev_mod", "");
            jsonObject2.put("devIPv4", "");
            jsonObject2.put("devIPv6", "");
            jsonObject2.put("devMAC", "");
            jsonObject2.put("device_id", "");
            jsonObject3.put("busiAnnotations", jsonObject);
            jsonObject3.put("commonAnnotations", jsonObject2);
            jsonObjects.add(jsonObject3);
        }
        return jsonObjects;
    }

    public List<JSONObject> getOperationLogs2(SimpleDateFormat simpleDateFormat, String now) {
        List<Map<String, Object>> operationLogs = bfmUserMapper.getOperationLogs2();
        List<JSONObject> jsonObjects = new ArrayList<>();
        for (Map<String, Object> operationLogMap : operationLogs) {
            JSONObject jsonObject = new JSONObject();   // 操作日志信息
            JSONObject jsonObject2 = new JSONObject();  // 日志通用字段
            JSONObject jsonObject3 = new JSONObject();  // 汇总JSON
            String id = MapUtils.getString(operationLogMap, "id");
            jsonObject.put("logId", id);
            jsonObject.put("clientIp", "");
            jsonObject.put("clientMac", "");
            jsonObject.put("clientType", "WEB");
            jsonObject.put("clientDeviceId", "");
            jsonObject.put("account", MapUtils.getString(operationLogMap, "user_code"));
            jsonObject.put("phone", MapUtils.getString(operationLogMap, "phone"));
            jsonObject.put("unit", "【天翼云】大数据和AI中心");
            jsonObject.put("unitId", "8500066");
            jsonObject.put("department", "");
            jsonObject.put("position", "");
            jsonObject.put("businessIp", "127.0.0.1");
            jsonObject.put("businessSystemName", "【天翼云】中国电信重庆公司信息服务业务系统AI通用平台");
            jsonObject.put("businessSystemId", "10069");
            String create_time = simpleDateFormat.format(operationLogMap.get("create_time"));
            String update_time = simpleDateFormat.format(operationLogMap.get("update_time"));
            String status = MapUtils.getString(operationLogMap, "status");
            String name = MapUtils.getString(operationLogMap, "service_name");
            if (create_time.equals(update_time)) {
                jsonObject.put("operate_type", "1");
                jsonObject.put("operate_desc", "【天翼云】新增推理能力" + name + "成功");
                jsonObject.put("operate_command", "INSERT INTO `ai_factory_inference`.`air_service` (`id`, `service_name`, `status`,  `create_time`, `update_time`) VALUES (" + id + ",'" + name + "','" + "'5'" + "','" + create_time + "','" + update_time + ");");
            } else if ("9".equals(status)) {
                jsonObject.put("operate_type", "2");
                jsonObject.put("operate_desc", "【天翼云】删除推理能力" + name + "成功");
                jsonObject.put("operate_command", "delete from `ai_factory_inference`.`air_service` where id=" + id + ";");
            } else {
                jsonObject.put("operate_type", "3");
                jsonObject.put("operate_desc", "【天翼云】修改推理能力" + name + "成功");
                jsonObject.put("operate_command", "update `ai_factory_inference`.`air_service` set 'service_name'=" + name + ",update_time=" + update_time + " where id=" + id);
            }
            jsonObject.put("operate_time", create_time);
            jsonObject.put("result", "1");
            jsonObject.put("operate_param", "");
            jsonObject.put("operate_business", "推理能力");
            jsonObject.put("operate_level", "1");
            jsonObject.put("operate_sensitive", "0");
            jsonObject.put("operate_reason", "0");
            jsonObject.put("operate_reason_desc", "");
            jsonObject.put("operate_vn", "");
            jsonObject.put("SensitiveLevel", "D6-1");
            jsonObject.put("ExportStaff", "");
            jsonObject.put("ExportTime", "");
            jsonObject.put("ExportFileName", "");

            jsonObject2.put("log_province", "85");
            jsonObject2.put("logTime", create_time);
            jsonObject2.put("logID", MapUtils.getString(operationLogMap, "id"));
            jsonObject2.put("Log_type", "026");
            jsonObject2.put("equ_manuf", "");
            jsonObject2.put("dev_mod", "");
            jsonObject2.put("devIPv4", "");
            jsonObject2.put("devIPv6", "");
            jsonObject2.put("devMAC", "");
            jsonObject2.put("device_id", "");
            jsonObject3.put("busiAnnotations", jsonObject);
            jsonObject3.put("commonAnnotations", jsonObject2);
            jsonObjects.add(jsonObject3);
        }
        return jsonObjects;
    }

    public List<JSONObject> getOperationLogs4(SimpleDateFormat simpleDateFormat, String now) {
        List<Map<String, Object>> operationLogs = bfmUserMapper.getOperationLogs4();
        List<JSONObject> jsonObjects = new ArrayList<>();
        for (Map<String, Object> operationLogMap : operationLogs) {
            JSONObject jsonObject = new JSONObject();   // 操作日志信息
            JSONObject jsonObject2 = new JSONObject();  // 日志通用字段
            JSONObject jsonObject3 = new JSONObject();  // 汇总JSON
            String id = MapUtils.getString(operationLogMap, "id");
            jsonObject.put("logId", id);
            jsonObject.put("clientIp", "");
            jsonObject.put("clientMac", "");
            jsonObject.put("clientType", "WEB");
            jsonObject.put("clientDeviceId", "");
            jsonObject.put("account", MapUtils.getString(operationLogMap, "user_code"));
            jsonObject.put("phone", MapUtils.getString(operationLogMap, "phone"));
            jsonObject.put("unit", "【天翼云】大数据和AI中心");
            jsonObject.put("unitId", "8500066");
            jsonObject.put("department", "");
            jsonObject.put("position", "");
            jsonObject.put("businessIp", "127.0.0.1");
            jsonObject.put("businessSystemName", "【天翼云】中国电信重庆公司信息服务业务系统AI通用平台");
            jsonObject.put("businessSystemId", "10069");
            String create_time = simpleDateFormat.format(operationLogMap.get("create_time"));
            String update_time = simpleDateFormat.format(operationLogMap.get("update_time"));
            String status = MapUtils.getString(operationLogMap, "status");
            String name = MapUtils.getString(operationLogMap, "service_name");
            if (create_time.equals(update_time)) {
                jsonObject.put("operate_type", "1");
                jsonObject.put("operate_desc", "【天翼云】新增推理能力" + name + "版本成功");
                jsonObject.put("operate_command", "INSERT INTO `ai_factory_inference`.`air_service_version` (`id`, `service_code`, `status`,  `create_time`, `update_time`) VALUES (" + id + ",'" + "'2.2'" + "','" + "'5'" + "','" + create_time + "','" + update_time + ");");
            } else if ("9".equals(status)) {
                jsonObject.put("operate_type", "2");
                jsonObject.put("operate_desc", "【天翼云】删除推理能力" + name + "版本成功");
                jsonObject.put("operate_command", "delete from `ai_factory_inference`.`air_service_version` where id=" + id + ";");
            } else {
                jsonObject.put("operate_type", "3");
                jsonObject.put("operate_desc", "【天翼云】修改推理能力" + name + "版本成功");
                jsonObject.put("operate_command", "update `ai_factory_inference`.`air_service_version` set update_time=" + update_time + " where id = " + id);
            }
            jsonObject.put("operate_time", create_time);
            jsonObject.put("result", "1");
            jsonObject.put("operate_param", "");
            jsonObject.put("operate_business", "推理能力版本");
            jsonObject.put("operate_level", "1");
            jsonObject.put("operate_sensitive", "0");
            jsonObject.put("operate_reason", "0");
            jsonObject.put("operate_reason_desc", "");
            jsonObject.put("operate_vn", "");
            jsonObject.put("SensitiveLevel", "D6-1");
            jsonObject.put("ExportStaff", "");
            jsonObject.put("ExportTime", "");
            jsonObject.put("ExportFileName", "");

            jsonObject2.put("log_province", "85");
            jsonObject2.put("logTime", create_time);
            jsonObject2.put("logID", MapUtils.getString(operationLogMap, "id"));
            jsonObject2.put("Log_type", "026");
            jsonObject2.put("equ_manuf", "");
            jsonObject2.put("dev_mod", "");
            jsonObject2.put("devIPv4", "");
            jsonObject2.put("devIPv6", "");
            jsonObject2.put("devMAC", "");
            jsonObject2.put("device_id", "");
            jsonObject3.put("busiAnnotations", jsonObject);
            jsonObject3.put("commonAnnotations", jsonObject2);
            jsonObjects.add(jsonObject3);
        }
        return jsonObjects;
    }

    @Override
    public List<JSONObject> getOperationLogs3(SimpleDateFormat simpleDateFormat, String now) {
        List<Map<String, Object>> operationLogs = bfmUserMapper.getOperationLogs3();
        List<JSONObject> jsonObjects = new ArrayList<>();
        for (Map<String, Object> operationLogMap : operationLogs) {
            JSONObject jsonObject = new JSONObject();   // 操作日志信息
            JSONObject jsonObject2 = new JSONObject();  // 日志通用字段
            JSONObject jsonObject3 = new JSONObject();  // 汇总JSON
            String id = MapUtils.getString(operationLogMap, "task_id");
            jsonObject.put("logId", id);
            jsonObject.put("clientIp", "");
            jsonObject.put("clientMac", "");
            jsonObject.put("clientType", "WEB");
            jsonObject.put("clientDeviceId", "");
            jsonObject.put("account", MapUtils.getString(operationLogMap, "user_code"));
            jsonObject.put("phone", MapUtils.getString(operationLogMap, "phone"));
            jsonObject.put("unit", "【天翼云】大数据和AI中心");
            jsonObject.put("unitId", "8500066");
            jsonObject.put("department", "");
            jsonObject.put("position", "");
            jsonObject.put("businessIp", "127.0.0.1");
            jsonObject.put("businessSystemName", "【天翼云】中国电信重庆公司信息服务业务系统AI通用平台");
            jsonObject.put("businessSystemId", "10069");
            String create_time = simpleDateFormat.format(operationLogMap.get("create_time"));
            String update_time = simpleDateFormat.format(operationLogMap.get("update_time"));
            String status = MapUtils.getString(operationLogMap, "task_state");
            String name = MapUtils.getString(operationLogMap, "task_name");
            if (create_time.equals(update_time)) {
                jsonObject.put("operate_type", "1");
                jsonObject.put("operate_desc", "【天翼云】新增标注任务" + name + "成功");
                jsonObject.put("operate_command", "INSERT INTO `ai_factory_db`.`tai_mark_task` (`task_id`, `task_name`, `task_state`,  `create_time`, `update_time`) VALUES (" + id + ",'" + name + "','" + "'5'" + "','" + create_time + "','" + update_time + ");");
            } else if ("3".equals(status)) {
                jsonObject.put("operate_type", "2");
                jsonObject.put("operate_desc", "【天翼云】删除标注任务" + name + "成功");
                jsonObject.put("operate_command", "delete from `ai_factory_db`.`tai_mark_task` where task_id=" + id + ";");
            } else {
                jsonObject.put("operate_type", "3");
                jsonObject.put("operate_desc", "【天翼云】修改标注任务" + name + "成功");
                jsonObject.put("operate_command", "update `ai_factory_db`.`tai_mark_task` set 'service_name'=" + name + ",update_time=" + update_time + " where task_id=" + id);
            }
            jsonObject.put("operate_time", create_time);
            jsonObject.put("result", "1");
            jsonObject.put("operate_param", "");
            jsonObject.put("operate_business", "标注任务");
            jsonObject.put("operate_level", "1");
            jsonObject.put("operate_sensitive", "0");
            jsonObject.put("operate_reason", "0");
            jsonObject.put("operate_reason_desc", "");
            jsonObject.put("operate_vn", "");
            jsonObject.put("SensitiveLevel", "D6-1");
            jsonObject.put("ExportStaff", "");
            jsonObject.put("ExportTime", "");
            jsonObject.put("ExportFileName", "");

            jsonObject2.put("log_province", "85");
            jsonObject2.put("logTime", create_time);
            jsonObject2.put("logID", MapUtils.getString(operationLogMap, "task_id"));
            jsonObject2.put("Log_type", "026");
            jsonObject2.put("equ_manuf", "");
            jsonObject2.put("dev_mod", "");
            jsonObject2.put("devIPv4", "");
            jsonObject2.put("devIPv6", "");
            jsonObject2.put("devMAC", "");
            jsonObject2.put("device_id", "");
            jsonObject3.put("busiAnnotations", jsonObject);
            jsonObject3.put("commonAnnotations", jsonObject2);
            jsonObjects.add(jsonObject3);
        }
        return jsonObjects;
    }

}
